<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      v-model="form"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="onLoad(page, query)"
      @on-load="onLoad"
    >
      <template slot="processIsFinished" slot-scope="{ row }">
        <el-tag size="mini">{{
          row.processIsFinished === 'terminate' ? '已终结' : row.processIsFinished === 'withdraw' ? '已撤消' : '已完成'
        }}</el-tag>
      </template>
      <template slot="menu" slot-scope="{ row }">
        <el-link :underline="false" v-if="permission.wf_process_done_detail" type="primary" @click="handleDetail(row)"
          >详情</el-link
        >&nbsp;
        <el-link :underline="false" v-if="permission.wf_process_done_follow" type="primary" @click="handleFlow(row)"
          >流程图</el-link
        >
      </template>
    </avue-crud>

    <el-dialog
      :visible.sync="bpmnVisible"
      append-to-body
      destroy-on-close
      title="流程图"
      width="70%"
      custom-class="wf-dialog"
    >
      <wf-design ref="bpmn" style="height: 60vh" :options="bpmnOption"></wf-design>
    </el-dialog>
  </basic-container>
</template>

<script>
import { doneList as getList, detail } from '@/api/plugin/workflow/process'
import { mapGetters } from 'vuex'

import FlowForm from './mixins/flow-form'

export default {
  mixins: [FlowForm],
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        size: 'mini',
        height: 'auto',
        calcHeight: 30,
        tip: false,
        border: true,
        selection: true,
        dialogType: 'drawer',
        addBtn: false,
        editBtn: false,
        delBtn: false,
        align: 'center',
        searchMenuSpan: 6,
        searchSize: 'mini',
        searchIndex: 3,
        searchIcon: true,
        column: [
          {
            label: '流程名称',
            prop: 'processDefinitionName',
            search: true,
            overHidden: true
          },
          {
            label: '流程标识',
            prop: 'processDefinitionKey',
            search: true,
            overHidden: true
          },
          {
            label: '流水号',
            prop: 'serialNumber',
            bind: 'variables.serialNumber',
            search: true,
            overHidden: true
          },
          {
            label: '流程分类',
            type: 'tree',
            row: true,
            dicUrl: '/api/blade-workflow/design/category/tree',
            props: {
              label: 'name',
              value: 'id'
            },
            prop: 'category',
            search: true
          },
          {
            label: '当前节点',
            prop: 'taskName'
          },
          {
            label: '申请人',
            prop: 'startUsername',
            search: true
          },
          {
            label: '申请时间',
            prop: 'createTime',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm',
            width: 165
          },
          {
            label: '流程状态',
            prop: 'processIsFinished',
            dicData: [
              {
                label: '已完成',
                value: 'finished'
              },
              {
                label: '已终结',
                value: 'terminate'
              },
              {
                label: '已撤销',
                value: 'withdraw'
              }
            ],
            type: 'select',
            search: true,
            slot: true
          }
        ]
      },
      data: [],
      bpmnVisible: false,
      bpmnOption: {}
    }
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.deployment_add, false),
        viewBtn: this.vaildData(this.permission.deployment_view, false),
        delBtn: this.vaildData(this.permission.deployment_delete, false),
        editBtn: this.vaildData(this.permission.deployment_edit, false)
      }
    },
    ids() {
      let ids = []
      this.selectionList.forEach(ele => {
        ids.push(ele.id)
      })
      return ids.join(',')
    }
  },
  methods: {
    handleDetail(row) {
      this.$quickDrawer({
        title: '流程详情',
        width: '90%',
        component: require('@/views/desk/flow/ops/index').default,
        props: {
          type: 'view',
          param: {
            taskId: row.taskId,
            processInsId: row.processInstanceId,
            formUrl: row.formUrl
          }
        },
        on: {
          refresh: () => {
            this.onLoad(this.page)
          }
        }
      })
    },
    handleFlow(row) {
      const { taskId, processInstanceId } = row
      detail({ taskId, processInsId: processInstanceId }).then(res => {
        const { process, flow } = res.data.data

        this.bpmnOption = {
          mode: 'view',
          xml: process.xml,
          flows: this.handleResolveFlows(flow)
        }

        this.bpmnVisible = true
      })
    },
    searchReset() {
      this.query = {}
      this.onLoad(this.page)
    },
    searchChange(params, done) {
      this.query = params
      this.page.currentPage = 1
      this.onLoad(this.page, params)
      done()
    },
    selectionChange(list) {
      this.selectionList = list
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    onLoad(page, params = {}) {
      this.loading = true
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data
        this.page.total = data.total
        this.data = data.records
        this.loading = false
      })
    }
  }
}
</script>

<style></style>
